import {
    httpService
} from '@/common/http/index.js';
const utils = {
    /*-----------------------------计算当前时间-----------------------------*/
    getCurrentTime() {
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1;
        var day = today.getDate();
        var hour = today.getHours();
        var minute = today.getMinutes();
        var second = today.getSeconds();
        month = this._format(month);
        day = this._format(day);
        hour = this._format(hour);
        minute = this._format(minute);
        second = this._format(second);
        var dateText = year + "年" + month + "月" + day + "日";
        var timeText = hour + ":" + minute + ":" + second;
        return {
            dateText: dateText,
            timeText: timeText
        }
    },
    _format(time) {
        return time < 10 ? '0' + time : time;
    },
    /*-----------------------------返回当天的开始时间和结束时间-----------------------------*/
    getTodayStartAndEndDate(days) {
        var now = new Date();
        var start = now.getFullYear() + "-" + ((now.getMonth() + 1) >= 10 ? (now.getMonth() + 1) : '0' + (now.getMonth() + 1)) + "-" + (now.getDate() >= 10 ? now.getDate() : '0' + now.getDate()) + ' 00:00:00';
        var end = now.getFullYear() + "-" + ((now.getMonth() + 1) >= 10 ? (now.getMonth() + 1) : '0' + (now.getMonth() + 1)) + "-" + (now.getDate() >= 10 ? now.getDate() : '0' + now.getDate()) + ' 23:59:59';
        return {
            beginTime: start,
            endTime: end
        };
    },
    /*-----------------------------导出excel操作-----------------------------*/
    //导出
    exportUtil(api, obj) {
        let _accessToken = localStorage.getItem('token') ? localStorage.getItem('token') : null;
        obj.accessToken = _accessToken;
        window.location = httpService + api + this.urlTransform(obj);
    },
    //参数处理
    urlTransform(jsonObj) {
        var urlStr = '';
        var arr = [];
        for (var key in jsonObj) {
            arr.push(key + '=' + jsonObj[key]);
        }
        if (arr.length > 0) {
            for (var i = 0; i < arr.length; i++) {
                if (i == 0) {
                    urlStr += '?' + arr[i];
                } else {
                    urlStr += '&' + arr[i];
                }
            }
        }
        return urlStr;
    }
}
export default utils;
